<template>
  <div>
    <el-col
      v-for="btn in btnGroups"
      :key="btn.name"
      :span="8"
      class="table-button"
    >
      <el-button
        v-if="isShowByRowState(row, btn)"
        :type="'text'"
        size="mini"
        @click="btn.method(row)"
      >
        {{ btn.name }}
      </el-button>
    </el-col>
  </div>
</template>

<script>
/**
 * 计划状态
 * P_DRAFT(10,"草稿"),
P_WAIT_AUDIT(11,"待审批"),
P_AUDIT_FAIL(12,"审批失败"),
P_START(13,"启用"),
P_STOP(14,"停用"),
P_DELETED(15,"已删除"),
 */

/**
 * 任务状态
 *
T_STARTING(21,"进行中"),
T_COMPLETE(22,"已完成"),
T_TIME_OUT(23,"超时未完成"),
T_TRANSFER(24,"转派中"),
 */

/**
 * 任务项状态
 *
I_STARTING(31, "进行中"),
I_COMPLETE(32, "已完成"),
I_TIME_OUT(33, "超时未完成"),
I_TRANSFER(34,"转派中"),
I_CONFIRM(35,"待确认"),
I_DEFECT(36,"不合格");
I_DEFECT(40,"超时完成");
 */
//   v-if="isShowByRow(row,btn)"
const constBtns = [
  {
    name: "审批",
    btnType: "approve",
    cssType: "default",
    location: "table",
    showInpage: 2, // 第一级页面显示该按钮
  },
  {
    name: "分配",
    btnType: "allocate",
    cssType: "default",
    location: "table",
    showInpage: 2, // 第一级页面显示该按钮
  },
  {
    name: "挂起审核",
    btnType: "awaitapprove",
    cssType: "default",
    location: "table",
    showInpage: 2, // 第一级页面显示该按钮
  },
  {
    name: "验收",
    btnType: "check",
    cssType: "default",
    location: "table",
    showInpage: 2, // 第一级页面显示该按钮
  },
];
// 按钮类型

const stateInfo = {
  approve: [11], // 待审批可用
  edit: [10, 14, 12, 0, 1], // 停用 审批失败
  delete: [10, 11, 12, 14, 1, 0],
  using: [14, 0], // 在账号管理的地方有个enable字段分别为0，1
  useless: [13, 1],
  allocate: [37], // 待分配
  changedate: [31], //
  awaitapprove: [39], // 挂起审核
  check: [35],
};
export default {
  props: ["row"],
  data() {
    return {
      btnGroups: [],
    };
  },
  mounted() {
    console.log("button3");
    // this.btnGroups=this.$store.state.button.currentMenuButton
    this.btnGroups = this.$store.state.button.currentMenuButton.filter((e) => {
      return e.location == "table";
    });
    // this.btnGroups = this.btnGroups.concat(constBtns)
  },
  methods: {
    // 判断是否是该业务人 分为审批 改期 分配 挂起审批等特殊按钮
    isShowByUserId(row, btn) {
      if (btn.btnType == "approve") {
        if (
          row.approveUser &&
          row.approveUser.includes(this.$store.state.user.userId)
        ) {
          return true;
        } else {
          return false;
        }
        // 审批按钮
      }
      if (btn.btnType == "check") {
        if (
          row.checkMan &&
          row.checkMan.includes(this.$store.state.user.userId)
        ) {
          return true;
        } else {
          return false;
        }
      } // 验收按钮
      if (btn.btnType == "allocate") {
        if (row.disMan && row.disMan.includes(this.$store.state.user.userId)) {
          return true;
        } else {
          return false;
        }
      } // 审批挂起按钮
      return true;
    },
    // 由业务状态判断是否显示该按钮
    isShowByRowState(row, btn) {
      // 根据业务状态和按钮类型判断是否显示
      if (stateInfo[btn.btnType]) {
        // 受到单据状态控制
        const state = row.enable || 0; // 无状态时按照存草稿处理
        if (stateInfo[btn.btnType].includes(state)) {
          return this.isShowByUserId(row, btn);
        } else {
          return false;
        }
      } else {
        // 受到权限控制
        return true;
      }
    },
    defaultBtnMethod(row) {
      console.log(row);
    },
  },
};
</script>

<style>
</style>
